package com.szyt.basicframe.common;

import lombok.Getter;

import javax.swing.plaf.nimbus.State;

@Getter
public enum StatusEnum {
    // 成功状态码
    SUCCESS(200, "操作成功"),

    // 客户端错误状态码 4xx
    BAD_REQUEST(400, "请求参数错误"),
    UNAUTHORIZED(401, "未认证或Token已过期"),
    FORBIDDEN(403, "无权限访问"),
    NOT_FOUND(404, "资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方法不允许"),

    // 业务错误状态码 (可自定义)
    VALIDATE_FAILED(422, "参数验证失败"),
    ACCOUNT_LOCKED(423, "账号已被锁定"),
    ACCOUNT_DISABLED(424, "账号已被禁用"),
    ACCOUNT_EXPIRED(425, "账号已过期"),
    CREDENTIALS_EXPIRED(426, "凭证已过期"),

    // 服务器错误状态码 5xx
    INTERNAL_SERVER_ERROR(500, "服务器内部错误"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),

    // 自定义业务状态码 (6xxxx)
    BUSINESS_ERROR(60000, "业务异常"),
    DATA_NOT_FOUND(60001, "数据不存在"),
    DATA_ALREADY_EXISTS(60002, "数据已存在"),
    DATA_UPDATE_FAILED(60003, "数据更新失败"),
    DATA_DELETE_FAILED(60004, "数据删除失败"),
    DATA_INSERT_FAILED(60005, "数据新增失败"),
    VERIFICATION_CODE_EXPIRE(60006, "验证码错误或已过期"),

    // 用户相关错误 (61xxx)
    USER_NOT_LOGIN(61001, "用户未登录"),
    USER_LOGIN_FAILED(61002, "用户名或密码错误"),
    USER_ACCESS_DENIED(61003, "用户访问被拒绝"),
    USER_ACCOUNT_EXIST(61004, "账号已存在"),
    USER_ACCOUNT_NOT_EXIST(61005, "账号不存在"),

    // 文件上传相关 (62xxx)
    FILE_UPLOAD_ERROR(62001, "文件上传失败"),
    FILE_DOWNLOAD_ERROR(62002, "文件下载失败"),
    FILE_TOO_LARGE(62003, "文件过大"),
    FILE_TYPE_NOT_ALLOWED(62004, "文件类型不允许");

    private Integer code;
    private String msg;

    StatusEnum(Integer code,String msg) {
        this.code = code;
        this.msg=msg;
    }

    /**
     * 根据code获取ResultCode
     */
    public static StatusEnum getItemByCode(int code) {
        for (StatusEnum item : StatusEnum.values()) {
            if (item.getCode().intValue() == code) {
                return item;
            }
        }
        return null;
    }

    /**
     * 根据code获取message
     */
    public static String getMsgByCode(int code) {
        StatusEnum item = getItemByCode(code);
        return item == null ? null : item.getMsg();
    }
}
